package com.book.service.impl;

import com.book.entity.Dict;
import com.book.entity.User;
import com.book.format.Result;
import com.book.format.ResultUtil;
import com.book.service.LoginService;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Mikey on 2017/6/10.
 */
@Service
public class LoginServiceImpl implements LoginService {
    private String USERMAPPER = "com.book.mapper.UserMapper";
    private String DICTMAPPER = "com.book.mapper.DictMapper";

    @Resource
    private SqlSessionTemplate sqlSessionTemplate;

    public Result doLogin(String username, String password, HttpSession session){
        Map map = new HashMap();
        map.put("username",username);
        User user = sqlSessionTemplate.selectOne(USERMAPPER+".selectUserByUsername",map);
        if(user != null){
            if(user.getU_password().equals(password)){
                user.setU_password(null);
                session.setAttribute("user",user);
//                map.put("did",user.getU_type());
//                Dict dict = sqlSessionTemplate.selectOne(DICTMAPPER+".selectDictById",map);
                return ResultUtil.success(user);
            }else{
                return ResultUtil.error("1001","密码错误");
            }
        }else{
            return ResultUtil.error("1002","没有该用户");
        }
    }
}
